package com.web.exiaodao.remote.service.impl;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.entity.FestivalRemind;
import com.web.exiaodao.business.repository.FestivalRemindRepository;
import com.web.exiaodao.remote.service.ApiFestivalRemindService;
import com.web.exiaodao.remote.vo.FestivalRemindVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.DateUtils;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2015-5-15下午06:46:35
 * 类的说明：前端APIFestivalRemind接口实现类
 **/
@Service("ApiFestivalRemindService")
@Transactional(readOnly = false)
public class ApiFestivalRemindServiceImpl extends BaseService<FestivalRemind> implements ApiFestivalRemindService {

    @Autowired
    FestivalRemindRepository festivalRemindRepository;

    @Override
    public XaResult<FestivalRemindVo> findOne(Long tId) throws BusinessException {
        FestivalRemind obj = festivalRemindRepository.findByIdAndStatusNot(tId, XaConstant.Status.delete);
        XaResult<FestivalRemindVo> xr = new XaResult<FestivalRemindVo>();
        if (XaUtil.isNotEmpty(obj)) {
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), FestivalRemindVo.class));
            xr.getObject().setId(obj.getId());
        } else {
            throw new BusinessException(XaConstant.Message.object_not_find);
        }
        return xr;
    }

    @Override
    public XaResult<List<FestivalRemindVo>> findListNEStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示非删除的所有数据
            status = XaConstant.Status.delete;
        }
        filters.put("status", new SearchFilter("status", Operator.NE, status));
        Page<FestivalRemind> page = festivalRemindRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), FestivalRemind.class), pageable);
        XaResult<List<FestivalRemindVo>> xr = new XaResult<List<FestivalRemindVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), FestivalRemindVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<List<FestivalRemindVo>> findListEQStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示正常数据
            status = XaConstant.Status.valid;
        }
        filters.put("status", new SearchFilter("status", Operator.EQ, status));
        Page<FestivalRemind> page = festivalRemindRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), FestivalRemind.class), pageable);
        XaResult<List<FestivalRemindVo>> xr = new XaResult<List<FestivalRemindVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), FestivalRemindVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
            //// TODO:dz 2017/1/3 阴历
            //yyyy-mm-dd
            String  festivalDateStr = page.getContent().get(i).getFestivalDate();//阳历日期
            Date nowDate            = new Date();
            int curYear             = Integer.parseInt(DateUtils.getStrDate(nowDate, DateUtils.YYYY));
            Date festDate           = DateUtils.formatStr(curYear + "-" + festivalDateStr.split("-")[1] +"-"+ festivalDateStr.split("-")[2],DateUtils.YYYYMMDD);
            int diffDays            = DateUtils.daysOfTwo(nowDate, festDate);
            int  days               = diffDays >= 0  ? diffDays: (diffDays + DateUtils.getDaysInYear(nowDate));
            xr.getObject().get(i).setDaysDiffCount(days);
        }
        return xr;
    }

    @Override
    public XaResult<FestivalRemindVo> multiOperate(String modelIds,
                                                   Integer status) throws BusinessException {
        XaResult<FestivalRemindVo> xr = new XaResult<FestivalRemindVo>();
        if (status == null) {
            status = XaConstant.Status.delete;
        }
        if (modelIds != null) {
            String[] ids = modelIds.split(",");
            for (String id : ids) {
                FestivalRemind obj = festivalRemindRepository.findByIdAndStatusNot(Long.parseLong(id), status);
                if (XaUtil.isNotEmpty(obj)) {
                    obj.setStatus(status);
                    obj = festivalRemindRepository.save(obj);
                    xr.setObject(JSON.parseObject(JSON.toJSONString(obj), FestivalRemindVo.class));
                    xr.getObject().setId(obj.getId());
                } else {
                    throw new BusinessException(XaConstant.Message.object_not_find);
                }
            }
        }
        return xr;
    }

    @Override
    public XaResult<FestivalRemindVo> createModel(FestivalRemind model)
            throws BusinessException {
        XaResult<FestivalRemindVo> xr = new XaResult<FestivalRemindVo>();
        FestivalRemind obj = festivalRemindRepository.save(model);
        xr.setObject(JSON.parseObject(JSON.toJSONString(obj), FestivalRemindVo.class));
        xr.getObject().setId(obj.getId());
        return xr;
    }

    @Override
    public XaResult<FestivalRemindVo> updateModel(FestivalRemind model)
            throws BusinessException {
        FestivalRemind obj = festivalRemindRepository.findOne(model.getId());
        XaResult<FestivalRemindVo> xr = new XaResult<FestivalRemindVo>();
        if (XaUtil.isNotEmpty(obj)) {
            obj.setName(XaUtil.isNotEmpty(model.getName()) ? model.getName() : obj.getName());
            obj.setCellphone(XaUtil.isNotEmpty(model.getCellphone()) ? model.getCellphone() : obj.getCellphone());
            obj.setFestivalDate(XaUtil.isNotEmpty(model.getFestivalDate()) ? model.getFestivalDate() : obj.getFestivalDate());
            obj.setIsLunarCalendar(XaUtil.isNotEmpty(model.getIsLunarCalendar()) ? model.getIsLunarCalendar() : obj.getIsLunarCalendar());
            obj.setDaysDiff(XaUtil.isNotEmpty(model.getDaysDiff()) ? model.getDaysDiff() : obj.getDaysDiff());
            obj.setType(XaUtil.isNotEmpty(model.getType()) ? model.getType() : obj.getType());
            obj.setUserId(XaUtil.isNotEmpty(model.getUserId()) ? model.getUserId() : obj.getUserId());
            obj = festivalRemindRepository.save(obj);
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), FestivalRemindVo.class));
            xr.getObject().setId(obj.getId());
        } else {
            throw new BusinessException(XaConstant.Message.object_not_find);
        }
        return xr;
    }

}
